package com.example.controller;

import com.example.common.Result;
import com.example.entity.Hotel;
import com.example.service.HotelService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/hotel")
public class HotelController {
    @Resource
     private HotelService hotelService;

    @PostMapping
    public Result<?> save(@RequestBody Hotel hotel) {
        return Result.success(hotelService.save(hotel));
    }

    @PutMapping
    public Result<?> update(@RequestBody Hotel hotel) {
        return Result.success(hotelService.updateById(hotel));
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        hotelService.removeById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<Hotel> findById(@PathVariable Long id) {
        return Result.success(hotelService.getById(id));
    }

    @GetMapping
    public Result<List<Hotel>> findAll() {
        return Result.success(hotelService.list());
    }

    @GetMapping("/page")
    public Result<IPage<Hotel>> findPage(@RequestParam(required = false, defaultValue = "") String name,
                                           @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                           @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return Result.success(hotelService.page(new Page<>(pageNum, pageSize), Wrappers.<Hotel>lambdaQuery().like(Hotel::getName, name)));
    }

    @RequestMapping(value = "hotelSave",method = {RequestMethod.POST})
    public Result hotelSave(String itemGroupids,@RequestBody Hotel hotel){
        hotelService.addSetmealSave(itemGroupids,hotel);
        return Result.success();
    }

}